{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Defuse the Bomb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: decrypt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #拆炸弹"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你有一个炸弹需要拆除，时间紧迫！你的情报员会给你一个长度为 <code>n</code> 的 <strong>循环</strong> 数组 <code>code</code> 以及一个密钥 <code>k</code> 。</p>\n",
    "\n",
    "<p>为了获得正确的密码，你需要替换掉每一个数字。所有数字会 <strong>同时</strong> 被替换。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果 <code>k > 0</code> ，将第 <code>i</code> 个数字用 <strong>接下来</strong> <code>k</code> 个数字之和替换。</li>\n",
    "\t<li>如果 <code>k < 0</code> ，将第 <code>i</code> 个数字用 <strong>之前</strong> <code>k</code> 个数字之和替换。</li>\n",
    "\t<li>如果 <code>k == 0</code> ，将第 <code>i</code> 个数字用 <code>0</code> 替换。</li>\n",
    "</ul>\n",
    "\n",
    "<p>由于 <code>code</code> 是循环的， <code>code[n-1]</code> 下一个元素是 <code>code[0]</code> ，且 <code>code[0]</code> 前一个元素是 <code>code[n-1]</code> 。</p>\n",
    "\n",
    "<p>给你 <strong>循环</strong> 数组 <code>code</code> 和整数密钥 <code>k</code> ，请你返回解密后的结果来拆除炸弹！</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>code = [5,7,1,4], k = 3\n",
    "<b>输出：</b>[12,10,16,13]\n",
    "<b>解释：</b>每个数字都被接下来 3 个数字之和替换。解密后的密码为 [7+1+4, 1+4+5, 4+5+7, 5+7+1]。注意到数组是循环连接的。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>code = [1,2,3,4], k = 0\n",
    "<b>输出：</b>[0,0,0,0]\n",
    "<b>解释：</b>当 k 为 0 时，所有数字都被 0 替换。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>code = [2,4,9,3], k = -2\n",
    "<b>输出：</b>[12,5,6,13]\n",
    "<b>解释：</b>解密后的密码为 [3+9, 2+3, 4+2, 9+4] 。注意到数组是循环连接的。如果 k 是负数，那么和为 <strong>之前</strong> 的数字。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == code.length</code></li>\n",
    "\t<li><code>1 <= n <= 100</code></li>\n",
    "\t<li><code>1 <= code[i] <= 100</code></li>\n",
    "\t<li><code>-(n - 1) <= k <= n - 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [defuse-the-bomb](https://leetcode.cn/problems/defuse-the-bomb/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [defuse-the-bomb](https://leetcode.cn/problems/defuse-the-bomb/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[5,7,1,4]\\n3', '[1,2,3,4]\\n0', '[2,4,9,3]\\n-2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://leetcode.cn/problems/decrypt-string-from-alphabet-to-integer-mapping/\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        map = {\n",
    "            '1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i',\n",
    "            '10#':'j', '11#':'k', '12#':'l', '13#':'m', '14#':'n', '15#':'o', '16#':'p', '17#':'q', '18#':'r', '19#':'s',\n",
    "            '20#':'t', '21#':'u', '22#':'v', '23#':'w', '24#': 'x', '25#':'y', '26#':'z',\n",
    "        }\n",
    "        replacemap = {\n",
    "            '10#': 'j', '11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p', '17#': 'q', '18#': 'r',\n",
    "            '19#': 's',\n",
    "            '20#': 't', '21#': 'u', '22#': 'v', '23#': 'w', '24#': 'x', '25#': 'y', '26#': 'z',\n",
    "        }\n",
    "        for key, val in replacemap.items():\n",
    "            s = s.replace(key, val)\n",
    "        ans = \"\"\n",
    "        for c in s:\n",
    "            if c not in '0123456789':\n",
    "                ans += c\n",
    "            else:\n",
    "                ans += map[c]\n",
    "        return ans\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "#     s = Solution()\n",
    "#     for testcase in [\n",
    "#         \"10#11#12\",\n",
    "#         \"1326#\",\n",
    "#     ]:\n",
    "#         ret = s.freqAlphabets(testcase)\n",
    "#         print(\"testcase={}, ret={}\".format(testcase, ret))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        res=[]\n",
    "        for i in range(len(code)):\n",
    "            s=0\n",
    "            if k>0:\n",
    "                a=1\n",
    "            else:\n",
    "                a=-1\n",
    "            for j in range(abs(k)):\n",
    "                s+=code[(i+(j+1)*a)%len(code)]\n",
    "            res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k > 0:\n",
    "            t = sum(code[1:k+1])\n",
    "            s, e = 1, k+1\n",
    "        elif k < 0:\n",
    "            t = sum(code[k:])\n",
    "            s, e = k, 0\n",
    "        else:\n",
    "            return ans\n",
    "        \n",
    "        ans[0] = t\n",
    "        for i in range(1,n):\n",
    "            t += code[e%n] - code[s%n]\n",
    "            ans[i] = t\n",
    "            s, e = s + 1, e + 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = []\n",
    "        if k > 0:\n",
    "            for i in range(n):\n",
    "                res = 0\n",
    "                for j in range(i + 1, i + k + 1):\n",
    "                    res += code[j % n]\n",
    "                ans.append(res)\n",
    "        elif k < 0:\n",
    "            for i in range(n):\n",
    "                res = 0\n",
    "                for j in range(i - 1, i - 1 + k, -1):\n",
    "                    res += code[j % n]\n",
    "                ans.append(res)\n",
    "        else:\n",
    "            ans = [0] * n\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k==0:\n",
    "            return [0]*len(code)\n",
    "        elif k<0:\n",
    "            new_list =[]\n",
    "            start = len(code)\n",
    "            code.extend(code)\n",
    "            for i in range(start,len(code)):\n",
    "                new_list.append(sum(code[i-abs(k):i]))\n",
    "        elif k>0:\n",
    "            new_list =[]\n",
    "            code.extend(code)\n",
    "            for i in range(len(code)//2):\n",
    "                new_list.append(sum(code[i+1:i+k+1]))\n",
    "        return new_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        t, ans = 0, [0] * len(code)\n",
    "        for i, x in enumerate(code):\n",
    "            t = k\n",
    "            while i + t != i:\n",
    "                ans[i] += code[(i + t) % len(code)]\n",
    "                t = t - 1 if t > 0 else t + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k==0:\n",
    "            return [0]*len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code+=code\n",
    "        if k>0:\n",
    "            l,r = 1,k\n",
    "        else:\n",
    "            l,r =n+k,n-1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r+1]\n",
    "            l,r = l+1,r+1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "\n",
    "        if k < 0:\n",
    "            return self.NegativeDecrypt(code, abs(k))\n",
    "        \n",
    "        return self.PositiveDecrypt(code, k)\n",
    "\n",
    "    def PositiveDecrypt(self, code, k):\n",
    "        processList = [i for i in code] * 2\n",
    "\n",
    "        for i in range(len(code)):\n",
    "            code[i] = sum(processList[i + 1 : i + k + 1])\n",
    "\n",
    "        return code\n",
    "\n",
    "    def NegativeDecrypt(self, code, k):\n",
    "        processList = [i for i in code] * 2\n",
    "\n",
    "        codeLen = len(code)\n",
    "        for i in range(codeLen, codeLen * 2):\n",
    "            code[i - codeLen] = sum(processList[i - k : i])\n",
    "\n",
    "        return code\n",
    "\n",
    "        # result = [0] * len(code)\n",
    "        # if k == 0:\n",
    "        #     return result\n",
    "\n",
    "        # distance = k\n",
    "        # if k < 0:\n",
    "        #     code = code[::-1]\n",
    "        #     distance = abs(distance)\n",
    "\n",
    "        # codeLen = len(code)\n",
    "        # for i in range(codeLen):\n",
    "        #     for j in range(i + 1, i + distance + 1):\n",
    "        #         result[i] += code[j % codeLen]\n",
    "\n",
    "        # if k < 0:\n",
    "        #     return result[::-1]\n",
    "\n",
    "        # return result\n",
    "        \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        code,ans = code * 2,[]\n",
    "        l = len(code)\n",
    "        if k > 0:\n",
    "            for i in range(l//2):\n",
    "                ans.append(sum(code[i+1:i+k+1]))\n",
    "        elif k < 0:\n",
    "            for i in range(l//2,l):\n",
    "                ans.append(sum(code[i+k:i]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        if k == 0:\n",
    "            return [0] * n\n",
    "        if k > 0:\n",
    "            res = []\n",
    "            for i in range(n):\n",
    "                sum_temp = 0\n",
    "                for j in range(1, k + 1):\n",
    "                    sum_temp += code[(i + j) % n]\n",
    "                res.append(sum_temp)\n",
    "            return res\n",
    "        if k < 0:\n",
    "            res = []\n",
    "            for i in range(n):\n",
    "                sum_temp = 0\n",
    "                for j in range(1, -k + 1):\n",
    "                    sum_temp += code[(i - j)]\n",
    "                res.append(sum_temp)\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0]*n\n",
    "        if k==0:return ans\n",
    "        for i in range(n):\n",
    "            if k>0:\n",
    "                for j in range(i+1,i+k+1):\n",
    "                    ans[i] += code[j%n]\n",
    "            else:\n",
    "                for j in range(i+k,i):\n",
    "                    ans[i] += code[(j+n)%n]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                ans[i] = sum(code[j % n] for j in range(i+1, i+1+k))\n",
    "            elif k < 0:\n",
    "                ans[i] = sum(code[j % n] for j in range(i-1, i-1+k, -1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n=len(code)\n",
    "        boom=[0]*n\n",
    "        for i in range(n):\n",
    "            cnt=0\n",
    "            if k>0:\n",
    "                for j in range(i+1,i+k+1):\n",
    "                    boom[i]+=code[j%n]\n",
    "            elif k==0:\n",
    "                boom[i]+=0\n",
    "            elif k<0:\n",
    "                for j in range(i+k,i):\n",
    "                    boom[i]+=code[j]\n",
    "        return boom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        l = len(code)\n",
    "        if k == 0:\n",
    "            for i in range(l):  \n",
    "                code[i] = 0\n",
    "            return code\n",
    "            \n",
    "        new = code + code\n",
    "        if k > 0:\n",
    "            tmp = k\n",
    "            for i in range(0, l):\n",
    "                new[i] = 0 \n",
    "                while k > 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k -= 1  \n",
    "                k = tmp \n",
    "            return new[:l]\n",
    "        else:\n",
    "            tmp = k\n",
    "            for i in range(2*l - 1, l - 1, -1):\n",
    "                new[i] = 0 \n",
    "                while k < 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k += 1\n",
    "                k = tmp\n",
    "            return new[l:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        code = code * 3\n",
    "        return [sum(code[i+1:i+1+k]) if k > 0 else sum(code[i+k:i]) for i in range(n,n*2)] if k else [0] * n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code.extend(code)\n",
    "        if k > 0:\n",
    "            l, r = 1, k + 1\n",
    "        else:\n",
    "            l, r = n + k, n\n",
    "        w = sum(code[l:r])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r]\n",
    "            l += 1\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        n = len(code)\n",
    "        temp = code + code + code\n",
    "\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                res.append(sum(temp[n + i + 1:n + i + k + 1]))\n",
    "            else:\n",
    "                t = -k\n",
    "                res.append(sum(temp[n + i - t:n + i]))\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "\n",
    "        if k < 0:\n",
    "            return self.NegativeDecrypt(code, abs(k))\n",
    "        \n",
    "        return self.PositiveDecrypt(code, k)\n",
    "\n",
    "    def PositiveDecrypt(self, code, k):\n",
    "        processList = [i for i in code] * 2\n",
    "\n",
    "        for i in range(len(code)):\n",
    "            code[i] = sum(processList[i + 1 : i + k + 1])\n",
    "\n",
    "        return code\n",
    "\n",
    "    def NegativeDecrypt(self, code, k):\n",
    "        processList = [i for i in code] * 2\n",
    "\n",
    "        codeLen = len(code)\n",
    "        for i in range(codeLen, codeLen * 2):\n",
    "            code[i - codeLen] = sum(processList[i - k : i])\n",
    "\n",
    "        return code\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        s = list(accumulate(code + code, initial=0))\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                ans[i] = s[i + k + 1] - s[i + 1]\n",
    "            else:\n",
    "                ans[i] = s[i + n] - s[i + k + n]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        elif k > 0:\n",
    "            res = [0] * len(code)\n",
    "            for i in range(k):\n",
    "                code.append(code[i])\n",
    "            for i in range(len(code) - k):\n",
    "                res[i] = sum(code[i + 1: i + k + 1]) \n",
    "            return res\n",
    "        elif k < 0:\n",
    "            k = abs(k)\n",
    "            res = [0] * len(code)\n",
    "            for i in range(k):\n",
    "                code.insert(0, code[-1 * (i + 1)])\n",
    "            print(code)\n",
    "            for i in range(len(res)):\n",
    "                res[i] = sum(code[i : i + k]) \n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        ln = len(code)\n",
    "        ans = [0] * ln\n",
    "        flag = 1 if k > 0 else -1\n",
    "        for i in range(ln):\n",
    "            s = 0\n",
    "            for j in range(k * flag):\n",
    "                s += code[(i + (j + 1) * flag) % ln]\n",
    "            ans[i] = s\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "    #     if k == 0:\n",
    "    #         return [0] * len(code)\n",
    "\n",
    "    #     if k < 0:\n",
    "    #         return self.NegativeDecrypt(code, abs(k))\n",
    "        \n",
    "    #     return self.PositiveDecrypt(code, k)\n",
    "\n",
    "    # def PositiveDecrypt(self, code, k):\n",
    "    #     processList = [i for i in code] * 2\n",
    "\n",
    "    #     for i in range(len(code)):\n",
    "    #         code[i] = sum(processList[i + 1 : i + k + 1])\n",
    "\n",
    "    #     return code\n",
    "\n",
    "    # def NegativeDecrypt(self, code, k):\n",
    "    #     processList = [i for i in code] * 2\n",
    "\n",
    "    #     codeLen = len(code)\n",
    "    #     for i in range(codeLen, codeLen * 2):\n",
    "    #         code[i - codeLen] = sum(processList[i - k : i])\n",
    "\n",
    "    #     return code\n",
    "\n",
    "        result = [0] * len(code)\n",
    "        if k == 0:\n",
    "            return result\n",
    "\n",
    "        distance = k\n",
    "        if k < 0:\n",
    "            code = code[::-1]\n",
    "            distance = abs(distance)\n",
    "\n",
    "        codeLen = len(code)\n",
    "        for i in range(codeLen):\n",
    "            for j in range(i + 1, i + distance + 1):\n",
    "                result[i] += code[j % codeLen]\n",
    "\n",
    "        if k < 0:\n",
    "            return result[::-1]\n",
    "\n",
    "        return result\n",
    "        \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        l = len(code)\n",
    "        if k == 0:\n",
    "            for i in range(l):  \n",
    "                code[i] = 0\n",
    "            return code\n",
    "            \n",
    "        new = code + code\n",
    "        tmp = k\n",
    "        if k > 0:\n",
    "            for i in range(0, l):\n",
    "                new[i] = 0 \n",
    "                while k > 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k -= 1  \n",
    "                k = tmp \n",
    "            return new[:l]\n",
    "        else:\n",
    "            for i in range(2*l - 1, l - 1, -1):\n",
    "                new[i] = 0 \n",
    "                while k < 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k += 1\n",
    "                k = tmp\n",
    "            return new[l:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l : r + 1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        l = code.__len__()\n",
    "        def comp(code, k, index, length, flag):\n",
    "            ans = 0\n",
    "            cnt = 0\n",
    "            i = index\n",
    "            while cnt < k:\n",
    "                i = (i + flag) % length\n",
    "                # print(\"i={}, code[i]={}\".format(i, code[i]))\n",
    "                ans += code[i]\n",
    "                cnt += 1\n",
    "            return ans\n",
    "\n",
    "        if k == 0:\n",
    "            return [0] * l\n",
    "        elif k > 0:\n",
    "            ans = []\n",
    "            for i in range(l):\n",
    "                ans.append(comp(code, k, i, l, 1))\n",
    "        else:\n",
    "            ans = []\n",
    "            for i in range(l):\n",
    "                ans.append(comp(code, abs(k), i, l, -1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "\t\tlen_code = len(code)\n",
    "\t\tfinal = [0 for i in range(0, len_code)]\n",
    "\t\t\n",
    "\t\tif k == 0:\n",
    "\t\t\treturn final\n",
    "\t\t\n",
    "\t\tfor index in range(0, len_code):\n",
    "\t\t\tsum_index = 0\n",
    "\t\t\tfor k_index in range(1, abs(k) + 1):\n",
    "\t\t\t\tif k < 0:\n",
    "\t\t\t\t\tif index - k_index < 0:\n",
    "\t\t\t\t\t\tsum_index += code[index - k_index + len_code]\n",
    "\t\t\t\t\telse:\n",
    "\t\t\t\t\t\tsum_index += code[index - k_index]\n",
    "\t\t\t\tif k > 0:\n",
    "\t\t\t\t\tif index + k_index > len_code - 1:\n",
    "\t\t\t\t\t\tsum_index += code[index + k_index - len_code]\n",
    "\t\t\t\t\telse:\n",
    "\t\t\t\t\t\tsum_index += code[index + k_index]\n",
    "\t\t\tfinal[index] = sum_index\n",
    "\t\treturn final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        code_len = len(code)\n",
    "        if k == 0:\n",
    "            return code_len * [0]\n",
    "        elif k > 0:\n",
    "            i = 0\n",
    "            sum_res = sum(code[1:1+k])\n",
    "            res = []\n",
    "            while i < code_len:\n",
    "                res.append(sum_res)\n",
    "                i += 1\n",
    "                sum_res = sum_res + code[(i+k) % code_len] - code[i % code_len]\n",
    "        else:\n",
    "            i = 0\n",
    "            sum_res = sum(code[code_len+k:])\n",
    "            res = []\n",
    "            while i < code_len:\n",
    "                res.append(sum_res)\n",
    "                i += 1\n",
    "                sum_res = sum_res + code[(code_len+i-1) % code_len] - code[(code_len+i+k-1) % code_len]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        l = len(code)\n",
    "        if k == 0:\n",
    "            for i in range(l):  \n",
    "                code[i] = 0\n",
    "            return code\n",
    "            \n",
    "        new = code + code\n",
    "        if k > 0:\n",
    "            tmp = k\n",
    "            for i in range(0, l):\n",
    "                new[i] = 0 \n",
    "                while k > 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k -= 1  \n",
    "                k = tmp \n",
    "            return new[:l]\n",
    "        else:\n",
    "            tmp = k\n",
    "            for i in range(2*l - 1, l - 1, -1):\n",
    "                new[i] = 0 \n",
    "                while k < 0:\n",
    "                    new[i] += new[i + k]\n",
    "                    k += 1\n",
    "                k = tmp\n",
    "            return new[l:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        res=[]\n",
    "        if k==0:\n",
    "            return [0 for _ in range(len(code))]\n",
    "        elif k>0:\n",
    "            for i in range(len(code)):\n",
    "                if i+k<=len(code)-1:                    \n",
    "                    res.append(sum(code[i+1:i+k+1]))                    \n",
    "                else:                    \n",
    "                    res.append(sum(code[i+1:])+sum(code[:i+k-len(code)+1]))            \n",
    "        else:\n",
    "            t=-k\n",
    "            for i in range(len(code)):\n",
    "                if i-t>=0:                    \n",
    "                    res.append(sum(code[i-t:i])) \n",
    "                else:            \n",
    "                    res.append(sum(code[:i])+sum(code[i-t:])) \n",
    "        #print(res)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        #前缀和即可\n",
    "        n=len(code)\n",
    "        if k==0: return [0]*n\n",
    "        code=code*3\n",
    "        pre=list(accumulate(code,initial=0))\n",
    "        ans=[]\n",
    "        if k>0:\n",
    "            for i in range(n,2*n):\n",
    "                ans.append(pre[i+k+1]-pre[i+1])\n",
    "        else:\n",
    "            for i in range(n,2*n):\n",
    "                ans.append(pre[i]-pre[i+k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        ans = [0]*len(code)\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        elif k > 0:\n",
    "            excode = code+code[:k]\n",
    "            for i in range (len(code)):\n",
    "                ans[i] = sum(excode[i+1:i+1+k])\n",
    "        else:\n",
    "            excode = code[k:len(code)]+code\n",
    "            for i in range (len(code)-1,-1,-1):\n",
    "                ans [i] =sum(excode[i:i-k])\n",
    "        return ans            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        c=[0]*len(code)\n",
    "        s=[]\n",
    "        for i in range(abs(k)*2):\n",
    "            s+=code\n",
    "        if k==0:\n",
    "            return c\n",
    "        elif k>0:\n",
    "            for i in range(len(c)):\n",
    "                c[i]=sum(s[i+1:i+k+1])\n",
    "            return c\n",
    "        else:\n",
    "            a=len(code)*(abs(k)*2-1)\n",
    "            for i in range(len(c)):\n",
    "                c[i]=sum(s[a+i+k:a+i])\n",
    "            return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        code_len = len(code)\n",
    "        if k == 0:\n",
    "            return code_len * [0]\n",
    "        elif k > 0:\n",
    "            i = 0\n",
    "            sum_res = sum(code[1:1+k])\n",
    "            res = []\n",
    "            while i < code_len:\n",
    "                res.append(sum_res)\n",
    "                i += 1\n",
    "                sum_res = sum_res + code[(i+k) % code_len] - code[i % code_len]\n",
    "        else:\n",
    "            i = 0\n",
    "            sum_res = sum(code[code_len+k:])\n",
    "            res = []\n",
    "            while i < code_len:\n",
    "                res.append(sum_res)\n",
    "                sum_res = sum_res + code[(code_len+i) % code_len] - code[(code_len+i+k) % code_len]\n",
    "                i += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "    #     if k == 0:\n",
    "    #         return [0] * len(code)\n",
    "\n",
    "    #     if k < 0:\n",
    "    #         return self.NegativeDecrypt(code, abs(k))\n",
    "        \n",
    "    #     return self.PositiveDecrypt(code, k)\n",
    "\n",
    "    # def PositiveDecrypt(self, code, k):\n",
    "    #     processList = [i for i in code] * 2\n",
    "\n",
    "    #     for i in range(len(code)):\n",
    "    #         code[i] = sum(processList[i + 1 : i + k + 1])\n",
    "\n",
    "    #     return code\n",
    "\n",
    "    # def NegativeDecrypt(self, code, k):\n",
    "    #     processList = [i for i in code] * 2\n",
    "\n",
    "    #     codeLen = len(code)\n",
    "    #     for i in range(codeLen, codeLen * 2):\n",
    "    #         code[i - codeLen] = sum(processList[i - k : i])\n",
    "\n",
    "    #     return code\n",
    "\n",
    "        result = [0] * len(code)\n",
    "        if k == 0:\n",
    "            return result\n",
    "\n",
    "        distance = k\n",
    "        if k < 0:\n",
    "            code = code[::-1]\n",
    "            distance = abs(distance)\n",
    "\n",
    "        codeLen = len(code)\n",
    "        for i in range(codeLen):\n",
    "            for j in range(i + 1, i + distance + 1):\n",
    "                result[i] += code[j % codeLen]\n",
    "\n",
    "        if k < 0:\n",
    "            return result[::-1]\n",
    "\n",
    "        return result\n",
    "        \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        res = [0] * n\n",
    "        if k == 0:\n",
    "            return res\n",
    "        code.extend(code)\n",
    "        if k > 0:\n",
    "            l, r = 1, k + 1\n",
    "        else:\n",
    "            l, r = n + k, n\n",
    "        w = sum(code[l:r])\n",
    "        for i in range(n):\n",
    "            res[i] = w\n",
    "            w -= code[l]\n",
    "            w += code[r]\n",
    "            l += 1\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                right = i+k+1\n",
    "                ans[i] = sum(code[i+1:right])\n",
    "                if right > n:\n",
    "                    ans[i] += sum(code[:right - n])\n",
    "            else:\n",
    "                left = i+k\n",
    "                ans[i] = sum(code[left:i])\n",
    "                if left < 0:\n",
    "                    ans[i] = sum(code[:i]) + sum(code[left:])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k + 1\n",
    "        else:\n",
    "            l, r = n + k, n\n",
    "        w = sum(code[l:r])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r]\n",
    "            l += 1\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        temp = code.copy()\n",
    "        for i in range(n):\n",
    "            if k == 0:\n",
    "                code[i] = 0\n",
    "            elif k > 0:\n",
    "                cnt = 0\n",
    "                for j in range(i + 1, i + k + 1):\n",
    "                    j %= n\n",
    "                    cnt += temp[j]\n",
    "                code[i] = cnt\n",
    "            else:\n",
    "                cnt = 0\n",
    "                for j in range(i - 1, i - 1 + k, -1):\n",
    "                    j %= n\n",
    "                    cnt += temp[j]\n",
    "                code[i] = cnt\n",
    "        return code\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        if k == 0:return [0] * n\n",
    "        res = []\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l,r = 1,k\n",
    "        else:\n",
    "            l,r = n + k,n - 1\n",
    "        w = sum(code[l:r + 1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l += 1\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0]*len(code)\n",
    "        elif k > 0:\n",
    "            res, n = [], len(code)\n",
    "            code = code*2\n",
    "            for i in range(n):\n",
    "                tmp = 0\n",
    "                for j in range(i+1,i+k+1):\n",
    "                    tmp += code[j]\n",
    "                res.append(tmp)\n",
    "            return res\n",
    "        else:\n",
    "            res, n = [], len(code)\n",
    "            code = code[::-1]*2\n",
    "            k = -k\n",
    "            for i in range(n):\n",
    "                tmp = 0\n",
    "                for j in range(i+1,i+k+1):\n",
    "                    tmp += code[j]\n",
    "                res.append(tmp)\n",
    "            return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        if k > 0:\n",
    "            i, j = 1, k\n",
    "        else:\n",
    "            i, j = len(code) + k, len(code) - 1\n",
    "        window_sum = sum(code[i: j+1])\n",
    "        res.append(window_sum)\n",
    "        while len(res) < len(code):\n",
    "            i2 = i + 1 if i < len(code) - 1 else 0\n",
    "            j2 = j + 1 if j < len(code) - 1 else 0\n",
    "            window_sum = window_sum - code[i] + code[j2]\n",
    "            res.append(window_sum)\n",
    "            i, j = i2, j2\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        s = list(accumulate(chain(code, code), initial=0))\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                ans[i] = s[i + k + 1] - s[i + 1]\n",
    "            else:\n",
    "                ans[i] = s[i + n] - s[i + k + n]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        if k==0:\n",
    "            code = [0]*n\n",
    "        else:\n",
    "            if k>0:\n",
    "                code1 = code*2\n",
    "                for i in range(n):\n",
    "                    code[i]=sum(code1[i+1:i+k+1])\n",
    "            elif k<0:\n",
    "                code1 = code*2\n",
    "                for i in range(n-1,-1,-1):\n",
    "                    code[i]=sum(code1[n+i+k:n+i])\n",
    "        return code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        s = list(accumulate(code + code, initial=0))\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                ans[i] = s[i + k + 1] - s[i + 1]\n",
    "            else:\n",
    "                ans[i] = s[i + n] - s[i + k + n]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        res = [0 for _ in range(len(code))]\n",
    "        if k == 0:\n",
    "            return res\n",
    "        elif k>0:\n",
    "            code = code+code[:k]\n",
    "            for i in range(len(code)-k):\n",
    "                res[i] = sum(code[i+1:i+k+1])\n",
    "            return res\n",
    "        else:\n",
    "            code = code[k:]+code\n",
    "            for i in range(-k,len(code)):\n",
    "                res[i+k] = sum(code[i+k:i])\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                for j in range(i + 1, i + k + 1):\n",
    "                    ans[i] += code[j % n]\n",
    "            else:\n",
    "                for j in range(i + k, i):\n",
    "                    ans[i] += code[(j + n) % n]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        new_code = [0]*n\n",
    "        if k == 0:\n",
    "            return new_code\n",
    "        if k > 0:\n",
    "            for i in range(n):\n",
    "                val_sum = 0\n",
    "                for j in range(1, k+1):\n",
    "                    if i+j < n:\n",
    "                        val_sum += code[i+j]\n",
    "                    else:\n",
    "                        val_sum += code[i+j-n]\n",
    "                new_code[i] = val_sum\n",
    "            return new_code\n",
    "        if k < 0:\n",
    "            for i in range(n):\n",
    "                val_sum = 0\n",
    "                for j in range(1, abs(k)+1):\n",
    "                    if i-j >= 0:\n",
    "                        val_sum += code[i-j]\n",
    "                    else:\n",
    "                        val_sum += code[i-j+n]\n",
    "                new_code[i] = val_sum\n",
    "            return new_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n=len(code)\n",
    "        if k==0:\n",
    "            return [0]*n\n",
    "        res=[0]*n\n",
    "        if k>0:\n",
    "            for i in range(n):\n",
    "                if i+k <n:\n",
    "                    res[i]=sum(code[i+1:i+k+1])\n",
    "                else:\n",
    "                    res[i]=sum(code[i+1:])+sum(code[:i+k+1-n])\n",
    "        if k<0:\n",
    "            for i in range(n):\n",
    "                if i+k>=0:\n",
    "                    res[i]=sum(code[i+k:i])\n",
    "                else:\n",
    "                    res[i]=sum(code[:i])+sum(code[n+k+i:])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        code *= 2\n",
    "        res = [0]*n\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                res[i] = sum(code[i+1:i+k+1])\n",
    "            elif k < 0:\n",
    "                res[i] = sum(code[i+n+k:i+n])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        result = []\n",
    "        if k > 0:\n",
    "            for a in range(len(code)):\n",
    "                if len(code) - k - a - 1 >= 0:\n",
    "                    result.append(sum(code[a + 1:a + 1 + k]))\n",
    "                else:\n",
    "                    result.append(sum(code[a + 1:len(code)]) + sum(code[:k - len(code) + a + 1]))\n",
    "        elif k == 0:\n",
    "            result = [0 for _ in range(len(code))]\n",
    "        else:\n",
    "            k = -k\n",
    "            for b in range(len(code)):\n",
    "                if b < k:\n",
    "                    result.append(sum(code[b - k:] + code[:b]))\n",
    "                else:\n",
    "                    result.append(sum(code[b - k:b]))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n=len(code)\n",
    "        if k==0: return [0]*n\n",
    "        code=code*3\n",
    "        pre=list(accumulate(code,initial=0))\n",
    "        ans=[]\n",
    "        if k>0:\n",
    "            for i in range(n,2*n):\n",
    "                ans.append(pre[i+k+1]-pre[i+1])\n",
    "        else:\n",
    "            for i in range(n,2*n):\n",
    "                ans.append(pre[i]-pre[i+k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    @staticmethod\n",
    "    def calc_index(i, k, length):\n",
    "        if k > 0:\n",
    "            for index in range(i + 1, i + k + 1):\n",
    "                yield index % length\n",
    "        else:\n",
    "            print(i - 1, i + k)\n",
    "            for index in range(i - 1, i + k - 1, -1):\n",
    "                yield index % length\n",
    "\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        length = len(code)\n",
    "        if k == 0:\n",
    "            return [0] * length\n",
    "        ans = []\n",
    "        for i in range(length):\n",
    "            ans.append(sum(code[index] for index in self.calc_index(i, k, length)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        res = []\n",
    "        if k == 0:\n",
    "            return [0] * n\n",
    "        elif k > 0:\n",
    "            for i in range(n):\n",
    "                res.append(sum([ code[(j+i+1)%n ] for j in range(k) ]))\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                res.append(sum([code[ ((i-j-1)%n) ] for j in range(-k) ]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        if k == 0:\n",
    "            return [0] * n\n",
    "        code = code + code\n",
    "        ret = [0] * n\n",
    "        if k > 0:\n",
    "            for i in range(n):\n",
    "                ret[i] = sum(code[i+1:i+1+k])\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                ret[i] = sum(code[n+i+k:n+i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n =len(code)\n",
    "        ans = [0] * n\n",
    "        if k ==0:\n",
    "            return ans\n",
    "        s = list(accumulate(code + code,initial=0))\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                ans[i] = s[i+k+1]-s[i+1]\n",
    "            else:\n",
    "                ans[i] = s[i+n]-s[i+k+n]\n",
    "   \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        result = [0] * len(code)\n",
    "        if k == 0:\n",
    "            return result\n",
    "        for i in range(0,len(code)):\n",
    "            if k > 0:\n",
    "                for j in range(0,k):\n",
    "                    result[i] += code[(i+j+1)%len(code)]\n",
    "            elif k < 0:\n",
    "                for j in range(0,abs(k)):\n",
    "                    result[i] += code[(i-j-1)%len(code)]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        result = []\n",
    "        if k > 0:\n",
    "            for a in range(len(code)):\n",
    "                if len(code) - k - a - 1 >= 0:\n",
    "                    result.append(sum(code[a + 1:a + 1 + k]))\n",
    "                else:\n",
    "                    result.append(sum(code[a + 1:len(code)]) + sum(code[:k - len(code) + a + 1]))\n",
    "        elif k == 0:\n",
    "            result = [0 for _ in range(len(code))]\n",
    "        else:\n",
    "            k = -k\n",
    "            for b in range(len(code)):\n",
    "                if b < k:\n",
    "                    result.append(sum(code[b - k:] + code[:b]))\n",
    "                else:\n",
    "                    result.append(sum(code[b - k:b]))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        re = [0 for _ in range(len(code))]\n",
    "        if k==0:\n",
    "            return re\n",
    "        for i in range(len(code)):\n",
    "            \n",
    "            if k>0:\n",
    "                print(i)\n",
    "                if len(code)-i >k:\n",
    "                    re[i] = sum(code[i+1:i+k+1])\n",
    "                    # print(i,re,code[i+1:i+k+1])\n",
    "                else:\n",
    "                    # print(code[:k-(len(code)-(i+1))],code[i+1:])\n",
    "                    a = sum(code[i+1:])\n",
    "                    b = sum(code[:k-(len(code)-(i+1))])\n",
    "                    # print(a,b)\n",
    "                    re[i] = a+b\n",
    "                # print(\"lll\",re)\n",
    "            elif k<0:\n",
    "                \n",
    "                if i+k >=0:\n",
    "                    re[i] = sum(code[i+k:i])\n",
    "                else:\n",
    "                    a = sum(code[:i])\n",
    "                    b = sum(code[len(code)-(-k-i):])\n",
    "                    # print(i,a,b,code[:i],code[len(code)-(-k-i)-1:])\n",
    "                    re[i] = a+b\n",
    "        return re  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n = len(code)\n",
    "        ans = [0] * n\n",
    "        if k == 0:\n",
    "            return ans\n",
    "        for i in range(n):\n",
    "            if k > 0:\n",
    "                for j in range(i + 1, i + k + 1):\n",
    "                    ans[i] += code[j % n]\n",
    "            else:\n",
    "                for j in range(i + k, i):\n",
    "                    ans[i] += code[(j + n) % n]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        password=[]\n",
    "        m=len(code)\n",
    "        for i in range(m):\n",
    "            sum=0\n",
    "            if (k>0):\n",
    "                for j in range(i+1,i+k+1):\n",
    "                    if(j<m):\n",
    "                        sum=sum+code[j]\n",
    "                    elif(j>=m):\n",
    "                        sum+=code[j-m]\n",
    "            elif (k<0):\n",
    "                for j in range(i-1,i+k-1,-1):\n",
    "                    if(j<0):\n",
    "                        sum=sum+code[j]\n",
    "                    elif(j>=0):\n",
    "                        sum+=code[j]\n",
    "            else:\n",
    "                sum=0\n",
    "            password.append(sum)     \n",
    "        return(password)   \n",
    "    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        if k == 0:\n",
    "            return [0] * len(code)\n",
    "        res = []\n",
    "        n = len(code)\n",
    "        code += code\n",
    "        if k > 0:\n",
    "            l, r = 1, k\n",
    "        else:\n",
    "            l, r = n + k, n - 1\n",
    "        w = sum(code[l:r+1])\n",
    "        for i in range(n):\n",
    "            res.append(w)\n",
    "            w -= code[l]\n",
    "            w += code[r + 1]\n",
    "            l, r = l + 1, r + 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 第一层循环遍历数组的成员，第二层循环遍历K值\n",
    "    # 1、循环时索引值大于数组的长度时如何解决：\n",
    "    #  通过取余的操作解决 (curr_i + k_j)%len(code)\n",
    "    # 如果k < 0时，curr_i = curr_i + k 通过上面两个方法完成\n",
    "    def get_sum(self,code,idx,k):\n",
    "        n=len(code)\n",
    "        if k>0:\n",
    "            # 比如处理索引为 2 后面的三个数的和，那就要从 2+1 开始相加\n",
    "            begin=(idx+1)%n\n",
    "        else:\n",
    "            # k<0，从前面 k 个加\n",
    "            begin=(idx+k)%n\n",
    "            \n",
    "        subcode=[]\n",
    "        for i in range(abs(k)):\n",
    "            index=(begin+i)%n\n",
    "            subcode.append(code[index])\n",
    "        return subcode\n",
    "\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n=len(code)\n",
    "        res=[]\n",
    "        for i in range(n):\n",
    "            # 对应索引位置 的列表\n",
    "            subcode=self.get_sum(code,i,k)\n",
    "            res.append(sum(subcode))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_sum(self,code,idx,k):\n",
    "        n=len(code)\n",
    "        if k>0:\n",
    "            # 比如处理索引为 2 后面的三个数的和，那就要从 2+1 开始相加\n",
    "            begin=(idx+1)%n\n",
    "        else:\n",
    "            # k<0，从前面 k 个加\n",
    "            begin=(idx+k)%n\n",
    "            \n",
    "        subcode=[]\n",
    "        for i in range(abs(k)):\n",
    "            index=(begin+i)%n\n",
    "            subcode.append(code[index])\n",
    "        return subcode\n",
    "\n",
    "    def decrypt(self, code: List[int], k: int) -> List[int]:\n",
    "        n=len(code)\n",
    "        res=[]\n",
    "        for i in range(n):\n",
    "            # 对应索引位置 的列表\n",
    "            subcode=self.get_sum(code,i,k)\n",
    "            res.append(sum(subcode))\n",
    "        return res\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
